package top.wshape1.shiziapi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.wshape1.shiziapi.common.constant.BizCodeEnum;
import top.wshape1.shiziapi.common.constant.RedisConstant;
import top.wshape1.shiziapi.common.exception.CustomException;
import top.wshape1.shiziapi.common.utils.R;
import top.wshape1.shiziapi.project.common.constant.DocumentConstant;
import top.wshape1.shiziapi.project.domain.Mindmap;
import top.wshape1.shiziapi.project.domain.Project;
import top.wshape1.shiziapi.project.domain.vo.CreateDocVo;
import top.wshape1.shiziapi.project.domain.vo.SaveContentVo;
import top.wshape1.shiziapi.project.handler.LoginContextHandler;
import top.wshape1.shiziapi.project.mapper.MindmapMapper;
import top.wshape1.shiziapi.project.service.MindmapService;
import top.wshape1.shiziapi.project.service.ProjectService;
import top.wshape1.shiziapi.project.util.HotProjectUtil;

import java.util.Date;

/**
 * @author Wshape1
 * @description 针对表【t_project_mindmap】的数据库操作Service实现
 * @since 2025-01-07 19:18:08
 */
@Service
@RequiredArgsConstructor
public class MindmapServiceImpl extends ServiceImpl<MindmapMapper, Mindmap>
        implements MindmapService {

    private final MindmapMapper mindmapMapper;

    private final RedisTemplate<String, Object> redisTemplate;

    private final ProjectService projectService;

    @Override
    public Long create(CreateDocVo createDocVo) {
        Mindmap mindmap = new Mindmap();
        mindmap.setParentFolderId(createDocVo.getParentFolderId());
        mindmap.setName(createDocVo.getName());
        mindmap.setProjectId(createDocVo.getProjectId());
        mindmap.setOrder(createDocVo.getOrder());
        Date time = new Date();
        mindmap.setCreateTime(time);
        mindmap.setUpdateTime(time);

        mindmapMapper.insert(mindmap);

        return mindmap.getId();
    }

    @Override
    public void rename(Long id, String name) {
        Mindmap update = new Mindmap();
        update.setId(id);
        update.setName(name);
        update.setUpdateTime(new Date());
        mindmapMapper.updateById(update);
    }

    @Override
    public void delete(Long id) {
        Mindmap delete = new Mindmap();
        delete.setId(id);
        delete.setDeleted(1);
        delete.setUpdateTime(new Date());
        mindmapMapper.updateById(delete);
    }

    @Override
    public void updateOrderAndParentFolderId(Long id, Integer order, Long parentFolderId) {
        Mindmap update = new Mindmap();
        update.setId(id);
        update.setOrder(order);
        update.setParentFolderId(parentFolderId);
        update.setUpdateTime(new Date());
        mindmapMapper.updateById(update);
    }

    @Override
    public void updateOrder(Long id, Integer order) {
        Mindmap update = new Mindmap();
        update.setId(id);
        update.setOrder(order);
        update.setUpdateTime(new Date());
        mindmapMapper.updateById(update);
    }

    @Override
    public void clearProject(long projectId) {
        mindmapMapper.update(new Mindmap(), new LambdaUpdateWrapper<Mindmap>()
                .eq(Mindmap::getProjectId, projectId)
                .eq(Mindmap::getDeleted, 0)
                .set(Mindmap::getDeleted, 1));
    }

    @Override
    public String getContent(Long projectId, Long id) {

        // 先查询是否是热点项目
        Object o = redisTemplate.opsForHash().get(RedisConstant.PROJECT_HOT_KEY + projectId,
                RedisConstant.PROJECT_HOT_RTF_KEY + id);
        if (o != null) {
            return (String) o;
        }

        Project project = projectService.getById(projectId);
        if (project.getIsPrivate() == 1) {
            if (!projectService.hasPermissionToEdit(projectId, LoginContextHandler.getUserId()))
                throw new CustomException(R.instance(BizCodeEnum.PROJECT_PERMISSION_ERROR));
        }

        Mindmap mindmap = mindmapMapper.selectOne(new LambdaQueryWrapper<Mindmap>()
                .eq(Mindmap::getId, id)
                .eq(Mindmap::getDeleted, 0));

        return mindmap.getContent();

    }

    @Override
    public void SaveContent(SaveContentVo contentVo) {
        Object editObj = redisTemplate.opsForValue().get("startEdit:" + contentVo.getProjectId() + ":" + DocumentConstant.MINDMAP_TYPE + ":" + contentVo.getId());
        if (editObj == null || !editObj.toString().equals(String.valueOf(LoginContextHandler.getUserId()))) {
            throw new CustomException(R.instance(BizCodeEnum.DOC_NOT_IN_EDIT_ERROR));
        }

        if (!projectService.hasPermissionToEdit(contentVo.getProjectId(), LoginContextHandler.getUserId())) {
            throw new CustomException(R.instance(BizCodeEnum.PROJECT_PERMISSION_ERROR));
        }

        Mindmap update = new Mindmap();
        update.setId(contentVo.getId());
        update.setContent(contentVo.getContent());
        update.setUpdateTime(new Date());
        int i = mindmapMapper.updateById(update);

        // 更新热点项目缓存
        if (i > 0) {
            HotProjectUtil.updateCacheIfHot(redisTemplate, contentVo.getProjectId(),
                    RedisConstant.PROJECT_HOT_RTF_KEY + contentVo.getId(), contentVo.getContent());
        }
    }
}




